local modid = 'webber_the_trainer'

---@type widget_widget
local Widget = require 'widgets/widget'
---@type widget_image
local Image = require 'widgets/image'
---@type widget_imagebutton
local ImageButton = require 'widgets/imagebutton'
---@type widget_text
local Text = require 'widgets/text'
local TEMPLATES = require "widgets/redux/templates"

---@class widget_webbert_seedbox_seed : widget_widget
---@field bg widget_image # 背景
---@field seed_avatar widget_image # 种子图标
---@field seed_name widget_text # 种子名称
---@field plant_name widget_text # 植物名称
---@field num widget_text # 数量
---@field btn widget_imagebutton # 按钮
---@field btn_drop widget_imagebutton # 丢到地上的按钮
---@field hint widget_text # 提示
---@field nutrients_promoter widget_text # 催肥
---@field nutrients_compost widget_text # 堆肥
---@field nutrients_poop widget_text # 粪肥
---@overload fun(seedname:string,plantname:string,xml:string,tex:string,box:ent_webbert_seedbox,seed_prefab:PrefabID):widget_webbert_seedbox_seed
local webbert_seedbox_seed = Class(Widget,
---@param self widget_webbert_seedbox_seed
function(self,seedname,plantname,xml,tex,box,seed_prefab)
    Widget._ctor(self, 'webbert_seedbox_seed')

    local bg_w = 500
    self.bg = self:AddChild(Image('images/global_redux.xml','spinner_background_normal.tex', 'spinner_background_normal.tex'))
    self.bg:SetOnGainFocus(function ()
        self.bg:SetTexture('images/global_redux.xml','menu_focus.tex','menu_focus.tex')
        self.bg:SetSize(bg_w,64)
    end)
    self.bg:SetOnLoseFocus(function ()
        self.bg:SetTexture('images/global_redux.xml','spinner_background_normal.tex','spinner_background_normal.tex')
        self.bg:SetSize(bg_w,64)
    end)
    self.bg:SetSize(bg_w,64)
    self.bg:SetHRegPoint(ANCHOR_LEFT)
    self.bg:SetPosition(-50,0,0)

    self.seed_avatar = self:AddChild(Image(xml,tex,tex))
    self.seed_avatar:SetClickable(false) -- 让用户可以透过种子头像聚焦bg

    local x = 100
    self.seed_name = self:AddChild(Text(BODYTEXTFONT,30,seedname))
    self.seed_name:SetPosition(x,17)

    self.plant_name = self:AddChild(Text(BODYTEXTFONT,30,plantname))
    self.plant_name:SetPosition(x,-13)

    local num_x,region_x,num_offset = 80,160,30
    self.num = self:AddChild(Text(NUMBERFONT,35,'0'))
    self.num:SetPosition(x+num_x+region_x/2+num_offset,0)
    self.num:SetRegionSize(region_x,40)
    self.num:SetHAlign(ANCHOR_LEFT)

    -- 肥料
    self.nutrients_promoter = self:AddChild(Text(NUMBERFONT,30,'',{.36,1,.3,1}))
    self.nutrients_promoter:SetPosition(x+num_x,20)
    self.nutrients_compost = self:AddChild(Text(NUMBERFONT,30,'',{.28,.4,.25,1}))
    self.nutrients_compost:SetPosition(x+num_x,0)
    self.nutrients_poop = self:AddChild(Text(NUMBERFONT,30,'',{.32,.25,.12,1}))
    self.nutrients_poop:SetPosition(x+num_x,-20)

    local focus = 0
    self.btn = self:AddChild(TEMPLATES.StandardButton(function()
    end , 'T', {50,50}))
    self.btn:SetPosition(x+num_x+region_x+20,0)
    self.btn:SetOnGainFocus(function ()
        if self.hint then
            self.hint:SetPosition(x+num_x+region_x+20,60)
            self.hint:SetString(STRINGS.MOD_WEBBER_THE_TRAINER.UI.webbert_seedbox_seed.btn)
            self.hint:Show()
        end
        focus = focus + 1
    end)
    self.btn:SetOnLoseFocus(function ()
        focus = focus - 1
        if focus <= 0 then
            self.hint:Hide()
        end
    end)

    ---@cast box ent_webbert_seedbox
    ---@cast seed_prefab PrefabID
    self.btn:SetOnClick(
    function ()
        local replica_map = box and box.replica.webbert_seedbox and box.replica.webbert_seedbox.map
        if replica_map then
            local cur_num = replica_map[seed_prefab] or 0
            if cur_num > 0 then -- 超过0个才允许拿
                SendModRPCToServer(GetModRPC(modid,modid..'webbert_seedbox_btn'),box,seed_prefab)
            end
        end
    end)

    local scale,f_scale = .2,.24
    self.btn_drop = self:AddChild(ImageButton('images/button_icons.xml','update.tex','update.tex','update.tex','update.tex','update.tex'))
    self.btn_drop:SetFocusScale(f_scale,f_scale,f_scale)
    self.btn_drop:SetNormalScale(scale,scale,scale)
    self.btn_drop:SetPosition(x+num_x+region_x+20+50,0)
    -- self.btn_drop:SetTooltip(STRINGS.MOD_WEBBER_THE_TRAINER.UI.webbert_seedbox_seed.btn_drop)
    self.btn_drop:SetOnGainFocus(function ()
        if self.hint then
            self.hint:SetPosition(x+num_x+region_x+20+50,60)
            self.hint:SetString(STRINGS.MOD_WEBBER_THE_TRAINER.UI.webbert_seedbox_seed.btn_drop)
            self.hint:Show()
        end
        focus = focus + 1
    end)
    self.btn_drop:SetOnLoseFocus(function ()
        focus = focus - 1
        if focus <= 0 then
            self.hint:Hide()
        end
    end)
    self.btn_drop:SetOnClick(function ()
        local replica_map = box and box.replica.webbert_seedbox and box.replica.webbert_seedbox.map
        if replica_map then
            local cur_num = replica_map[seed_prefab] or 0
            if cur_num > 0 then -- 超过0个才允许拿
                SendModRPCToServer(GetModRPC(modid,modid..'webbert_seedbox_btn_drop'),box,seed_prefab)
            end
        end
    end)

    self.hint = self:AddChild(Text(BODYTEXTFONT, 35, ''))
    self.hint:Hide()
end)

---comment
---@param num integer|string
function webbert_seedbox_seed:SetSeedsNum(num)
    self.num:SetString(tostring(num))
end

---给所有吸收养分,多加一个减号,增加辨识度
---@param a any
---@return unknown
local function fix_num(a)
    return a>0 and ('+'..a) or ('-'..a)
end

function webbert_seedbox_seed:SetNutrients(a,b,c)
    self.nutrients_promoter:SetString(fix_num(tonumber(a)))
    self.nutrients_compost:SetString(fix_num(tonumber(b)))
    self.nutrients_poop:SetString(fix_num(tonumber(c)))
end

return webbert_seedbox_seed